<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>高级查询</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户类型</label>
                            <div class="layui-input-inline">
                                <select name="type"  >
                                    <option value="">请选择</option>
                                    <option value="1">管理员</option>
                                    <option value="0">普通用户</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">修改密码</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<!--<script type="text/html" id="imgTpl">-->
<!--    <img src="http://localhost:8888/{{ d.photo }}">-->
<!--</script>-->
<script>
    layui.use(['form', 'table','miniPage','element','axios'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            axios=layui.axios,
            miniPage = layui.miniPage;

        //重新渲染form
        form.render('select');
        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:8888/user/queryAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'username', width: 100, title: '用户名'},
                // {field: 'photo', width: 100, title: '图片',templet:'#imgTpl'},
                {field: 'type', width: 100, title: '用户类型',
                    templet:function (res){
                        if(res.type==0){
                            return '<span class="layui-badge layui-btn-warm">普通用户</span>';
                        }else{
                            return '<span class="layui-badge layui-bg-orange">管理员</span>';
                        }
                    }},

                {field: 'sex', width: 60, title: '性别'},
                {field: 'age', title: '年龄', width: 60},
                {field: 'email', width: 150, title: '邮箱'},
                {field: 'personalSay', width: 120, title: '格言'},
                {field: 'lastTime', width: 150, title: '最后登录'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var username=data.field.username;
            var type=data.field.type;
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    username: username,
                    type:type
                }
            }, 'data');

            return false;
        });

        //刷新加载  单页
        window.reload=function(){
            table.reload('currentTableId');
        }


        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/user/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['60%', '60%'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var data = table.checkStatus('currentTableId').data;
                if(data.length==0){
                    layer.msg("请选择要删除的记录信息");
                }else{
                    //获取要删除记录的id集合
                    var ids=getCheckId(data);
                    axios.get('user/deleteByIds?ids='+ids).then(function (response){
                        if(response.code==200){
                            layer.msg("删除成功");
                            // 刷新主页面 table一块
                            window.reload();
                        }
                        //关闭弹出信息
                        // layer.close(parentIndex);
                    }).catch(function (error){
                        layer.msg(data.msg);
                    })
                }
            }
        });


        /**
         * 获取删除id的集合
         */
         function getCheckId(data){
           var arr=new Array();
           for(var i=0;i<data.length;i++){
               arr.push(data[i].id);
           }
           return arr.join(",");
        }

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });


        /**
         * 设置form信息
          */
        function setFormValue(data){
            form.val("updatePwd",{
                id:data.id
            })
        }

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {   // 监听修改密码操作
                var content = miniPage.getHrefContent('page/user/updatePwd.html');
                // var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '修改密码',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    // offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                //设置值信息
                setFormValue(data);

                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    axios.get('http://localhost:8888/user/deleteByIds?ids='+data.id).then(function (response){
                        if(response.code==200){
                            layer.msg("删除成功");
                            // 刷新主页面 table一块
                            window.reload();
                        }else{
                            console.log(response)
                            layer.msg(response.msg);
                        }
                        //关闭弹出信息
                        // layer.close(parentIndex);
                    }).catch(function (error){
                        layer.msg(data.msg);
                    })
                    layer.close(index);
                });
            }
        });

    });
</script>